﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DECOR.BL.BE;
using DECOR.BL.BC;

namespace DECOR.UserWinApp.Sistema
{
    public partial class frmSistema : Form
    {
        public frmSistema()
        {
            InitializeComponent();
        }

        private void frmSistema_Load(object sender, EventArgs e)
        {
            this.listarSistemas();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frmAddSistema _frmAddSistema = new frmAddSistema();
            //_frmAddSistema.MdiParent = this.MdiParent;
            _frmAddSistema.ShowDialog();

            this.listarSistemas();
        }

        private void listarSistemas()
        {
            List<SistemaSAPBE> lst = SistemaSAPBC.Instance.listar();
            dgvSistemas.DataSource = lst;
            if (dgvSistemas.Columns.Count > 0)
            {
                dgvSistemas.Columns["Sistema_id"].HeaderText = "ID";
            }
        }

        private void eliminarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Confirma?", "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    foreach (DataGridViewRow dgvItem in dgvSistemas.SelectedRows)
                    {
                        int pSistema_id = Convert.ToInt32(dgvItem.Cells["Sistema_id"].Value.ToString());
                        SistemaSAPBC.Instance.eliminar(pSistema_id);
                    }
                    this.listarSistemas();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void editarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridViewRow dgvItem in dgvSistemas.SelectedRows)
                {
                    int pSistema_id = Convert.ToInt32(dgvItem.Cells["Sistema_id"].Value.ToString());
                    SistemaSAPBE objSistemaSAP = SistemaSAPBC.Instance.leer(pSistema_id);
                    frmAddSistema _frmAddSistema = new frmAddSistema(objSistemaSAP);
                    //_frmAddUsuarioWeb.MdiParent = this.MdiParent;
                    _frmAddSistema.ShowDialog();

                    this.listarSistemas();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
